package realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class CustromRealm extends AuthorizingRealm {

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        // 获取用户传递的用户名，如果存在该用户名则将数据库中该用户的用户名密码封装在simpleAuthenticationInfo中返回，之后
        // shiro会自动进行密码equals校验
        System.out.println(principal);
        if (principal.equals("maomao")) {
            // 参数1：数据库中用户名。     参数2：数据库中密码。  参数3：自定义realm的名字
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("maomao", "123456", this.getName());
            return simpleAuthenticationInfo;
        }
        return null;
    }
}
